<template>


  <el-container>

    <el-header>
      <el-form :inline="true" class="demo-form-inline">

        <el-form-item>
          <el-button type="primary" @click="appVisible=true;">添加</el-button>
        </el-form-item>

        <el-dialog title="添加应用" :visible.sync="appVisible">
          <el-form :model="appModel">
            <el-form-item label="应用名称">
              <el-input v-model="appModel.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="应用编码">
              <el-input v-model="appModel.code" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="应用描述">
              <el-input v-model="appModel.msg" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="appVisible = false;appModel={}">取 消</el-button>
            <el-button type="primary" @click="addApp" v-if="!appModel.id">确 定</el-button>
          </div>
        </el-dialog>

      </el-form>
    </el-header>

    <el-main style="width: 100%">
      <el-table :data="apps" border style="width: 100%">
        <el-table-column
          prop="name"
          label="名称">
        </el-table-column>
        <el-table-column
          prop="code"
          label="编码">
        </el-table-column>
        <el-table-column
          prop="msg"
          label="描述">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="appVisible=true;appModel=scope.row">查看</el-button>
            <el-button type="text" size="small" @click="delApp(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
  </el-container>


</template>
<script>


  export default {
    name: "Application",
    mounted() {
      this.getApps();
    },
    data() {
      return {
        appVisible: false,
        apps: [],
        appModel: {}
      }
    },
    methods: {
      getApps() {
        this.$http.get("/back/apps").then((data) => {
          this.apps = data;
        }).catch(error => {
          console.error(error)
        });
      },
      addApp() {
        this.$http.post("/back/app", this.appModel).then((data) => {
          this.apps = data;
          this.appVisible = false;
        }).catch(error => {
          this.$message.error("添加应用异常！")
        });
      },
      delApp(app) {
        this.$http.delete("/back/delApp?appId=" + app.id).then((data) => {
          this.apps = data;
        }).catch(error => {
          this.$message.error("删除应用异常！")
        });
      }
    },
  }
</script>
